package com.ctrip.ec.biz.dao.forward;

import com.ctrip.ec.dal.DaoBase;
import com.ctrip.ec.biz.entity.forward.ForwardActivityEntity;
import com.ctrip.ec.dal.DbAccess;
import com.ctrip.ec.dal.IdConvert;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;

@Service
public class ForwardActivityDao extends DaoBase<ForwardActivityEntity> {
    public ForwardActivityDao() {
        super(ForwardActivityEntity.class);
    }

    public ForwardActivityDao(DbAccess db) {
        super(ForwardActivityEntity.class, db);
    }

    public ForwardActivityEntity entity(Long agentId, Long activityId) throws SQLException {
        String sql = "select * from " + this.getTableName() + " where agent_id=? and activity_id=?";
        ForwardActivityEntity entity = this.db.getScalar(ForwardActivityEntity.class, sql, agentId, activityId);
        return entity;
    }

    public void deleteByActivityIds(List<Long> activityIds) throws SQLException {
        if (activityIds != null && activityIds.size() > 0) {
            String strActivityIds = IdConvert.join(activityIds);
            String sql = "delete from " + this.getTableName() + " where activity_id in (" + strActivityIds + ")";
            this.db.execute(sql);
        }
    }

    public boolean existActivity(Long agentId, Long activityId) throws SQLException {
        String sql = "select count(*) from " + this.getTableName() + " where agent_id=? and activity_id=?";
        Long count = this.db.getScalar(Long.class,sql,agentId,activityId);
        return count>0;
    }

    public ForwardActivityEntity entityByActivityId(Long agentId, Long activityId) throws SQLException {
        String sql = "select * from " + this.getTableName() + " where agent_id=? and activity_id=?";
        ForwardActivityEntity entity = this.db.getScalar(ForwardActivityEntity.class,sql,agentId,activityId);
        return entity;
    }
}
